﻿// -----------------------------------------------------------------------
// <copyright file="SampleTaskManager.cs" company="The Usual Dosage">
// © 2013 by Matthew M. Martin
// </copyright>
// -----------------------------------------------------------------------

namespace Cadence.NET.Examples
{
    using System;
    using System.Collections.Generic;
    using Cadence.NET.BaseClasses;
    using Cadence.NET.Calendars;
    using Cadence.NET.Interfaces;

    /// <summary>
    /// An example implementation of a CadenceTaskManager.
    /// </summary>
    public class SampleTaskManager : CadenceTaskManager
    {
        public SampleTaskManager()
        {
            // Create a sample task and apply the context and event listeners
            SampleTask sampleTask = new SampleTask();
            SampleTaskContext sampleContext = new SampleTaskContext()
            {
                TaskCompletedNotificationMessage = string.Format("SampleTask completed successfully."),
                TaskStartedNotificationMessage = string.Format("SampleTask has started."),
            };

            sampleTask.TaskContext = sampleContext;

            // Add our event handlers
            sampleTask.TaskError += new Task.TaskErrorEventHandler(TaskError);
            sampleTask.TaskStarted += new Task.TaskStartedEventHandler(TaskStarted);
            sampleTask.TaskCompleted += new Task.TaskCompletedEventHandler(TaskCompleted);

            sampleTask.Calendars = new List<ICalendar>();

            // Only run the tasks on Mondays and Wednesdays, in EST timezone
            sampleTask.Calendars.Add(new DailyCalendar(Monday: true, Wednesday: true, Friday: true) { TimezoneUTCOffset = -4 });

            // Only run the tasks between 9:00AM and 2:30PM EST
            sampleTask.Calendars.Add(new HourlyCalendar() { StartTime = new Time(hour: 9), EndTime = new Time(hour: 14, minute: 30), TimezoneUTCOffset = -4 });

            // Create a new task list and add our task
            Tasks = new List<Task>();
            Tasks.Add(sampleTask);

            // Set the interval to run every 15 seconds forever, with exact precision, meaning the interval will adjust itself
            // based on run times of the tasks.
            Interval = new Interval()
            {
                Schedule = new Time() { Second = 15 },
                RunOptions = Cadence.NET.Interval.IntervalRunOptions.RunInfinite,
                Precision = Cadence.NET.Interval.IntervalPrecision.Exact
            };
        }

        protected void TaskCompleted(object sender, ITaskContext context)
        {
            // Do some cleanup after the task completes
            Log.Info("Task completed");
            context.LastRunCompleted = DateTime.Now;
            if (context.RunDuration.HasValue)
            {
                Log.ErrorFormat("Last run took {0}m, {1}s", context.RunDuration.Value.Seconds, context.RunDuration.Value.Seconds);
            }
            return;
        }

        protected void TaskStarted(object sender, ITaskContext context)
        {
            // Do some action when the task starts
            context.LastRunStarted = DateTime.Now;
            Log.Info("Task started");
            return;
        }

        protected void TaskError(object sender, ITaskContext context)
        {
            // Log the error to the default logger
            Log.Error(context.ErrorContext);
        }
    }
}
